// pages/message/message.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rightIcon: '/images/icon-right.png',
    message:[],
    page: 1,
    loadingHidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var list = wx.getStorageSync('message');
    if (list.length<1){
      this.getMessage();
    }else{
      this.setData({
        message: list
      })
    }
    
  },


 

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page + 1;
    that.setData({
      page: page
    })
    that.getMessage();
  },

  /**
   * 公告详情
   */
  goMessageDetails: function (e) {
    console.log(e);
    var m_id = e.currentTarget.dataset.m_id;
    var list = this.data.message;
  
    for (var i = 0; i < list.length;i++){
    
      if (list[i].nid == m_id){
        list[i].status = 1;
      }
    }
    wx.setStorageSync('message', list);
    var lists = wx.getStorageSync('message');
    this.setData({
      message: lists
    })
    wx.navigateTo({
      url: '../messageDetails/messageDetails?m_id='+m_id,
    })
  },
  //是否有未读的公告
  getMessage() {
    var that = this;
    that.setData({
      loadingHidden: false
    })
    app.ajax({
      url: 'Partner/Console/message',
      data: {
        "page": that.data.page,
        "row": 10
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (that.data.page > 1) {
            var list = that.data.message;
          } else {
            var list = [];
          }
          res.data.data.platform_bulletin.forEach(function (v, k) {
            if(v['status']&&v['status'] != 0){

            }else{
              v['status'] = 0;
            }
            list.push(v);
          })
          wx.setStorageSync('message', list);
          var lists = wx.getStorageSync('message');
          that.setData({
            message: lists,
          })
        }
        if (res.data.data.platform_bulletin.length == 0) {
          app.showToast('没有更多了');
        } 
      },
    })
  }
})